package com.jsu.springboot_medicine.controller;

import com.fasterxml.jackson.databind.annotation.JsonAppend;
import com.github.pagehelper.PageInfo;
import com.jsu.springboot_medicine.common.Result;
import com.jsu.springboot_medicine.common.StatusCode;
import com.jsu.springboot_medicine.entity.Dept;
import com.jsu.springboot_medicine.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController{
    @Autowired
    private DeptService deptService;
    @GetMapping("/All/{pageNum}")
    public Result<PageInfo> selectAll(@PathVariable("pageNum") Integer pageNum){
        PageInfo<Dept> orders=deptService.selectAll(pageNum);
        return new Result<PageInfo>(true, StatusCode.OK,"查询成功",orders);
    }
    //根据id修改商品
    @PutMapping("/{id}")
    public Result<String> update(@Validated @RequestBody Dept dept, BindingResult bindingResult,
                                 @PathVariable("deptId") Integer deptId) {
//        //检查项目
//        if (bindingResult.hasErrors()) {
//            StringBuffer stringBuffer = new StringBuffer();
//            List<ObjectError> allErrors = bindingResult.getAllErrors();
//            for (ObjectError objectError : allErrors) {
//                stringBuffer.append(objectError.getDefaultMessage()).append("; ");
//            }
//            String s = stringBuffer.toString();
//            System.out.println(s);
//            return new Result<String>(false, StatusCode.ERROR, "修改失败",s);
//        }
//        order.setUpdateTime(new Date());
//        order.setOrderId(id);
        deptService.updateByPrimaryKey(deptId);
        return new Result(true, StatusCode.OK, "修改成功",null);
    }
    @PostMapping
    public Result<String> insert(@Valid @RequestBody Dept dept, BindingResult bindingResult) {
        //检查项目
//        if (bindingResult.hasErrors()) {
//            StringBuffer stringBuffer = new StringBuffer();
//            List<ObjectError> allErrors = bindingResult.getAllErrors();
//            for (ObjectError objectError : allErrors) {
//                stringBuffer.append(objectError.getDefaultMessage()).append("; ");
//            }
//            String s = stringBuffer.toString();
//            System.out.println(s);
//            return new Result<String>(false, StatusCode.ERROR, "添加失败",s);
//        }
//        //获取用户名
//        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        String name = principal.getUsername();
//        order.setOwnName(name);
//        //设置时间
//        order.setCreateTime(new Date());
//        order.setUpdateTime(new Date());
//        //添加
        deptService.insert(dept);
        return new Result(true, StatusCode.OK, "添加成功",null);
    }

}
